home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209b.zip / octave-2.09 / doc / octave.i05 (.txt) < prev    next >
GNU Info File  |  1997-08-20  |  49KB  |  945 lines

  1. This is Info file octave, produced by Makeinfo-1.64 from the input file
  2. octave.tex.
  3. START-INFO-DIR-ENTRY
  4. * Octave: (octave).     Interactive language for numerical computations.
  5. END-INFO-DIR-ENTRY
  6.    Copyright (C) 1996, 1997 John W. Eaton.
  7.    Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided that
  12. the entire resulting derived work is distributed under the terms of a
  13. permission notice identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions.
  17. File: octave,  Node: Script Files,  Next: Dynamically Linked Functions,  Prev: Function Files,  Up: Functions and Scripts
  18. Script Files
  19. ============
  20.    A script file is a file containing (almost) any sequence of Octave
  21. commands.  It is read and evaluated just as if you had typed each
  22. command at the Octave prompt, and provides a convenient way to perform a
  23. sequence of commands that do not logically belong inside a function.
  24.    Unlike a function file, a script file must *not* begin with the
  25. keyword `function'.  If it does, Octave will assume that it is a
  26. function file, and that it defines a single function that should be
  27. evaluated as soon as it is defined.
  28.    A script file also differs from a function file in that the variables
  29. named in a script file are not local variables, but are in the same
  30. scope as the other variables that are visible on the command line.
  31.    Even though a script file may not begin with the `function' keyword,
  32. it is possible to define more than one function in a single script file
  33. and load (but not execute) all of them at once.  To do this, the first
  34. token in the file (ignoring comments and other white space) must be
  35. something other than `function'.  If you have no other statements to
  36. evaluate, you can use a statement that has no effect, like this:
  37.      # Prevent Octave from thinking that this
  38.      # is a function file:
  39.      
  40.      1;
  41.      
  42.      # Define function one:
  43.      
  44.      function one ()
  45.        ...
  46.    To have Octave read and compile these functions into an internal
  47. form, you need to make sure that the file is in Octave's `LOADPATH',
  48. then simply type the base name of the file that contains the commands.
  49. (Octave uses the same rules to search for script files as it does to
  50. search for function files.)
  51.    If the first token in a file (ignoring comments) is `function',
  52. Octave will compile the function and try to execute it, printing a
  53. message warning about any non-whitespace characters that appear after
  54. the function definition.
  55.    Note that Octave does not try to look up the definition of any
  56. identifier until it needs to evaluate it.  This means that Octave will
  57. compile the following statements if they appear in a script file, or
  58. are typed at the command line,
  59.      # not a function file:
  60.      1;
  61.      function foo ()
  62.        do_something ();
  63.      endfunction
  64.      function do_something ()
  65.        do_something_else ();
  66.      endfunction
  67. even though the function `do_something' is not defined before it is
  68. referenced in the function `foo'.  This is not an error because Octave
  69. does not need to resolve all symbols that are referenced by a function
  70. until the function is actually evaluated.
  71.    Since Octave doesn't look for definitions until they are needed, the
  72. following code will always print `bar = 3' whether it is typed directly
  73. on the command line, read from a script file, or is part of a function
  74. body, even if there is a function or script file called `bar.m' in
  75. Octave's `LOADPATH'.
  76.      eval ("bar = 3");
  77.      bar
  78.    Code like this appearing within a function body could fool Octave if
  79. definitions were resolved as the function was being compiled.  It would
  80. be virtually impossible to make Octave clever enough to evaluate this
  81. code in a consistent fashion.  The parser would have to be able to
  82. perform the call to `eval' at compile time, and that would be
  83. impossible unless all the references in the string to be evaluated could
  84. also be resolved, and requiring that would be too restrictive (the
  85. string might come from user input, or depend on things that are not
  86. known until the function is evaluated).
  87.    Although Octave normally executes commands from script files that
  88. have the name `FILE.m', you can use the function `source' to execute
  89. commands from any file.
  90.  - Built-in Function:  source (FILE)
  91.      Parse and execute the contents of FILE.  This is equivalent to
  92.      executing commands from a script file, but without requiring the
  93.      file to be named `FILE.m'.
  94. File: octave,  Node: Dynamically Linked Functions,  Next: Organization of Functions,  Prev: Script Files,  Up: Functions and Scripts
  95. Dynamically Linked Functions
  96. ============================
  97.    On some systems, Octave can dynamically load and execute functions
  98. written in C++.  Octave can only directly call functions written in C++,
  99. but you can also load functions written in other languages by calling
  100. them from a simple wrapper function written in C++.
  101.    Here is an example of how to write a C++ function that Octave can
  102. load, with commentary.  The source for this function is included in the
  103. source distributions of Octave, in the file `examples/oregonator.cc'.
  104. It defines the same set of differential equations that are used in the
  105. example problem of *Note Ordinary Differential Equations::.  By running
  106. that example and this one, we can compare the execution times to see
  107. what sort of increase in speed you can expect by using dynamically
  108. linked functions.
  109.    The function defined in `oregonator.cc' contains just 8 statements,
  110. and is not much different than the code defined in the corresponding
  111. M-file (also distributed with Octave in the file
  112. `examples/oregonator.m').
  113.    Here is the complete text of `oregonator.cc':
  114.    just
  115.      #include <octave/oct.h>
  116.      
  117.      DEFUN_DLD (oregonator, args, ,
  118.        "The `oregonator'.")
  119.      {
  120.        ColumnVector dx (3);
  121.      
  122.        ColumnVector x = args(0).vector_value ();
  123.      
  124.        dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0)
  125.                         - 8.375e-06*pow (x(0), 2));
  126.      
  127.        dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27;
  128.      
  129.        dx(2) = 0.161*(x(0) - x(2));
  130.      
  131.        return octave_value (dx);
  132.      }
  133.    The first line of the file,
  134.      #include <octave/oct.h>
  135. includes declarations for all of Octave's internal functions that you
  136. will need.  If you need other functions from the standard C++ or C
  137. libraries, you can include the necessary headers here.
  138.    The next two lines
  139.      DEFUN_DLD (oregonator, args, ,
  140.        "The `oregonator'.")
  141. declares the function.  The macro `DEFUN_DLD' and the macros that it
  142. depends on are defined in the files `defun-dld.h', `defun.h', and
  143. `defun-int.h' (these files are included in the header file
  144. `octave/oct.h').
  145.    Note that the third parameter to `DEFUN_DLD' (`nargout') is not
  146. used, so it is omitted from the list of arguments to in order to avoid
  147. the warning from gcc about an unused function parameter.
  148. simply declares an object to store the right hand sides of the
  149. differential equation, and
  150.    The statement
  151.      ColumnVector x = args(0).vector_value ();
  152. extracts a column vector from the input arguments.  The variable `args'
  153. is passed to functions defined with `DEFUN_DLD' as an
  154. `octave_value_list' object, which includes methods for getting the
  155. length of the list and extracting individual elements.
  156.    In this example, we don't check for errors, but that is not
  157. difficult.  All of the Octave's built-in functions do some form of
  158. checking on their arguments, so you can check the source code for those
  159. functions for examples of various strategies for verifying that the
  160. correct number and types of arguments have been supplied.
  161.    The next statements
  162.      ColumnVector dx (3);
  163.      
  164.      dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0)
  165.                       - 8.375e-06*pow (x(0), 2));
  166.      
  167.      dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27;
  168.      
  169.      dx(2) = 0.161*(x(0) - x(2));
  170. define the right hand side of the differential equation.  Finally, we
  171. can return `dx':
  172.      return octave_value (dx);
  173. The actual return type is `octave_value_list', but it is only necessary
  174. to convert the return type to an `octave_value' because there is a
  175. default constructor that can automatically create an object of that
  176. type from an `octave_value' object, so we can just use that instead.
  177.    To use this file, your version of Octave must support dynamic
  178. linking.  To find out if it does, type the command `octave_config_info
  179. ("dld")' at the Octave prompt.  Support for dynamic linking is included
  180. if this command returns 1.
  181.    To compile the example file, type the command `mkoctfile
  182. oregonator.cc' at the shell prompt.  The script `mkoctfile' should have
  183. been installed along with Octave.  Running it will create a file called
  184. `oregonator.oct' that can be loaded by Octave.  To test the
  185. `oregonator.oct' file, start Octave and type the command
  186.      oregonator ([1, 2, 3], 0)
  187. at the Octave prompt.  Octave should respond by printing
  188.      ans =
  189.      
  190.         77.269353
  191.         -0.012942
  192.         -0.322000
  193.    You can now use the `oregonator.oct' file just as you would the
  194. `oregonator.m' file to solve the set of differential equations.
  195.    On a 133 MHz Pentium running Linux, Octave can solve the problem
  196. shown in *Note Ordinary Differential Equations:: in about 1.4 second
  197. using the dynamically linked function, compared to about 19 seconds
  198. using the M-file.  Similar decreases in execution time can be expected
  199. for other functions, particularly those that rely on functions like
  200. `lsode' that require user-supplied functions.
  201.    Additional examples are available in the files in the `src'
  202. directory of the Octave distribution.  Currently, this includes the
  203. files
  204.      balance.cc   fft2.cc      inv.cc       qzval.cc
  205.      chol.cc      filter.cc    log.cc       schur.cc
  206.      colloc.cc    find.cc      lsode.cc     sort.cc
  207.      dassl.cc     fsolve.cc    lu.cc        svd.cc
  208.      det.cc       givens.cc    minmax.cc    syl.cc
  209.      eig.cc       hess.cc      pinv.cc
  210.      expm.cc      ifft.cc      qr.cc
  211.      fft.cc       ifft2.cc     quad.cc
  212. These files use the macro `DEFUN_DLD_BUILTIN' instead of `DEFUN_DLD'.
  213. The difference between these two macros is just that
  214. `DEFUN_DLD_BUILTIN' can define a built-in function that is not
  215. dynamically loaded if the operating system does not support dynamic
  216. linking.  To define your own dynamically linked functions you should use
  217. `DEFUN_DLD'.
  218.    There is currently no detailed description of all the functions that
  219. you can call in a built-in function.  For the time being, you will have
  220. to read the source code for Octave.
  221. File: octave,  Node: Organization of Functions,  Prev: Dynamically Linked Functions,  Up: Functions and Scripts
  222. Organization of Functions Distributed with Octave
  223. =================================================
  224.    Many of Octave's standard functions are distributed as function
  225. files.  They are loosely organized by topic, in subdirectories of
  226. `OCTAVE-HOME/lib/octave/VERSION/m', to make it easier to find them.
  227.    The following is a list of all the function file subdirectories, and
  228. the types of functions you will find there.
  229. `audio'
  230.      Functions for playing and recording sounds.
  231. `control'
  232.      Functions for design and simulation of automatic control systems.
  233. `elfun'
  234.      Elementary functions.
  235. `general'
  236.      Miscellaneous matrix manipulations, like `flipud', `rot90', and
  237.      `triu', as well as other basic functions, like `is_matrix',
  238.      `nargchk', etc.
  239. `image'
  240.      Image processing tools.  These functions require the X Window
  241.      System.
  242.      Input-ouput functions.
  243. `linear-algebra'
  244.      Functions for linear algebra.
  245. `miscellaneous'
  246.      Functions that don't really belong anywhere else.
  247. `plot'
  248.      A set of functions that implement the MATLAB-like plotting
  249.      functions.
  250. `polynomial'
  251.      Functions for manipulating polynomials.
  252. `set'
  253.      Functions for creating and manipulating sets of unique values.
  254. `signal'
  255.      Functions for signal processing applications.
  256. `specfun'
  257.      Special functions.
  258. `special-matrix'
  259.      Functions that create special matrix forms.
  260. `startup'
  261.      Octave's system-wide startup file.
  262. `statistics'
  263.      Statistical functions.
  264. `strings'
  265.      Miscellaneous string-handling functions.
  266. `time'
  267.      Functions related to time keeping.
  268. File: octave,  Node: Error Handling,  Next: Input and Output,  Prev: Functions and Scripts,  Up: Top
  269. Error Handling
  270. **************
  271.    Octave includes several functions for printing error and warning
  272. messages.  When you write functions that need to take special action
  273. when they encounter abnormal conditions, you should print the error
  274. messages using the functions described in this chapter.
  275.  - Built-in Function:  error (TEMPLATE, ...)
  276.      The `error' function formats the optional arguments under the
  277.      control of the template string TEMPLATE using the same rules as
  278.      the `printf' family of functions (*note Formatted Output::.).  The
  279.      resulting message is prefixed by the string `error: ' and printed
  280.      on the `stderr' stream.
  281.      Calling `error' also sets Octave's internal error state such that
  282.      control will return to the top level without evaluating any more
  283.      commands.  This is useful for aborting from functions or scripts.
  284.      If the error message does not end with a new line character,
  285.      Octave will print a traceback of all the function calls leading to
  286.      the error.  For example, given the following function definitions:
  287.           function f () g () end
  288.           function g () h () end
  289.           function h () nargin == 1 || error ("nargin != 1"); end
  290.      calling the function `f' will result in a list of messages that
  291.      can help you to quickly locate the exact location of the error:
  292.           f ()
  293.           error: nargin != 1
  294.           error: evaluating index expression near line 1, column 30
  295.           error: evaluating binary operator `||' near line 1, column 27
  296.           error: called from `h'
  297.           error: called from `g'
  298.           error: called from `f'
  299.      If the error message ends in a new line character, Octave will
  300.      print the message but will not display any traceback messages as
  301.      it returns control to the top level.  For example, modifying the
  302.      error message in the previous example to end in a new line causes
  303.      Octave to only print a single message:
  304.           function h () nargin == 1 || error ("nargin != 1\n"); end
  305.           f ()
  306.           error: nargin != 1
  307.  - Built-in Variable: error_text
  308.      This variable contains the the text of error messages that would
  309.      have been printed in the body of the most recent `unwind_protect'
  310.      or `try' statement or the TRY part of the most recent call to the
  311.      `eval' function.  Outside of the `unwind_protect' and `try'
  312.      statements or the `eval' function, or if no error has occurred
  313.      within them, the value of `error_text' is guaranteed to be the
  314.      empty string.
  315.      Note that the message does not include the first `error: ' prefix,
  316.      so that it may easily be passed to the `error' function without
  317.      additional processing(1).
  318.      *Note The try Statement:: and *Note The unwind_protect Statement::.
  319.  - Built-in Variable: beep_on_error
  320.      If the value of `beep_on_error' is nonzero, Octave will try to
  321.      ring your terminal's bell before printing an error message.  The
  322.      default value is 0.
  323.  - Built-in Function:  warning (MSG)
  324.      Print a warning message MSG prefixed by the string `warning: '.
  325.      After printing the warning message, Octave will continue to execute
  326.      commands.  You should use this function should when you want to
  327.      notify the user of an unusual condition, but only when it makes
  328.      sense for your program to go on.
  329.  - Built-in Function:  usage (MSG)
  330.      Print the message MSG, prefixed by the string `usage: ', and set
  331.      Octave's internal error state such that control will return to the
  332.      top level without evaluating any more commands.  This is useful for
  333.      aborting from functions.
  334.      After `usage' is evaluated, Octave will print a traceback of all
  335.      the function calls leading to the usage message.
  336.      You should use this function for reporting problems errors that
  337.      result from an improper call to a function, such as calling a
  338.      function with an incorrect number of arguments, or with arguments
  339.      of the wrong type.  For example, most functions distributed with
  340.      Octave begin with code like this
  341.           if (nargin != 2)
  342.             usage ("foo (a, b)");
  343.           endif
  344.      to check for the proper number of arguments.
  345.    The following pair of functions are of limited usefulness, and may be
  346. removed from future versions of Octave.
  347.  - Function File:  perror (NAME, NUM)
  348.      Print the error message for function NAME corresponding to the
  349.      error number NUM.  This function is intended to be used to print
  350.      useful error messages for those functions that return numeric error
  351.      codes.
  352.  - Function File:  strerror (NAME, NUM)
  353.      Return the text of an error message for function NAME
  354.      corresponding to the error number NUM.  This function is intended
  355.      to be used to print useful error messages for those functions that
  356.      return numeric error codes.
  357.    ---------- Footnotes ----------
  358.    (1)  Yes, it's a kluge, but it seems to be a reasonably useful one.
  359. File: octave,  Node: Input and Output,  Next: Plotting,  Prev: Error Handling,  Up: Top
  360. Input and Output
  361. ****************
  362.    There are two distinct classes of input and output functions.  The
  363. first set are modeled after the functions available in MATLAB.  The
  364. second set are modeled after the standard I/O library used by the C
  365. programming language and offer more flexibility and control over the
  366. output.
  367.    When running interactively, Octave normally sends any output intended
  368. for your terminal that is more than one screen long to a paging program,
  369. such as `less' or `more'.  This avoids the problem of having a large
  370. volume of output stream by before you can read it.  With `less' (and
  371. some versions of `more') you can also scan forward and backward, and
  372. search for specific items.
  373.    Normally, no output is displayed by the pager until just before
  374. Octave is ready to print the top level prompt, or read from the
  375. standard input (for example, by using the `fscanf' or `scanf'
  376. functions).  This means that there may be some delay before any output
  377. appears on your screen if you have asked Octave to perform a
  378. significant amount of work with a single command statement.  The
  379. function `fflush' may be used to force output to be sent to the pager
  380. (or any other stream) immediately.
  381.    You can select the program to run as the pager by setting the
  382. variable `PAGER', and you can turn paging off by setting the value of
  383. the variable `page_screen_output' to 0.
  384.  - Command: more
  385.  - Command: more ON
  386.  - Command: more OFF
  387.      Turn output pagination on or off.  Without an argument, `more'
  388.      toggles the current state.
  389.  - Built-in Variable: PAGER
  390.      The default value is normally `"less"', `"more"', or `"pg"',
  391.      depending on what programs are installed on your system.  *Note
  392.      Installation::.
  393.      When running interactively, Octave sends any output intended for
  394.      your terminal that is more than one screen long to the program
  395.      named by the value of the variable `PAGER'.
  396.  - Built-in Variable: page_screen_output
  397.      If the value of `page_screen_output' is nonzero, all output
  398.      intended for the screen that is longer than one page is sent
  399.      through a pager.  This allows you to view one screenful at a time.
  400.      Some pagers (such as `less'--see *Note Installation::) are also
  401.      capable of moving backward on the output.  The default value is 1.
  402.  - Built-in Variable: page_output_immediately
  403.      If the value of `page_output_immediately' is nonzero, Octave sends
  404.      output to the pager as soon as it is available.  Otherwise, Octave
  405.      buffers its output and waits until just before the prompt is
  406.      printed to flush it to the pager.  The default value is 0.
  407.       - Built-in Function:  fflush (FID)
  408.           Flush output to FID.  This is useful for ensuring that all
  409.           pending output makes it to the screen before some other event
  410.           occurs.  For example, it is always a good idea to flush the
  411.           standard output stream before calling `input'.
  412. * Menu:
  413. * Basic Input and Output::
  414. * C-Style I/O Functions::
  415. File: octave,  Node: Basic Input and Output,  Next: C-Style I/O Functions,  Prev: Input and Output,  Up: Input and Output
  416. Basic Input and Output
  417. ======================
  418. * Menu:
  419. * Terminal Output::
  420. * Terminal Input::
  421. * Simple File I/O::
  422. File: octave,  Node: Terminal Output,  Next: Terminal Input,  Prev: Basic Input and Output,  Up: Basic Input and Output
  423. Terminal Output
  424. ---------------
  425.    Since Octave normally prints the value of an expression as soon as it
  426. has been evaluated, the simplest of all I/O functions is a simple
  427. expression.  For example, the following expression will display the
  428. value of pi
  429.      pi
  430.           -| pi = 3.1416
  431.    This works well as long as it is acceptable to have the name of the
  432. variable (or `ans') printed along with the value.  To print the value
  433. of a variable without printing its name, use the function `disp'.
  434.    The `format' command offers some control over the way Octave prints
  435. values with `disp' and through the normal echoing mechanism.
  436.  - Built-in Variable: ans
  437.      This variable holds the most recently computed result that was not
  438.      explicitly assigned to a variable.  For example, after the
  439.      expression
  440.           3^2 + 4^2
  441.      is evaluated, the value of `ans' is 25.
  442.  - Built-in Function:  disp (X)
  443.      Display the value of X.  For example,
  444.           disp ("The value of pi is:"), disp (pi)
  445.           
  446.                -| the value of pi is:
  447.                -| 3.1416
  448.      Note that the output from `disp' always ends with a newline.
  449.  - Command: format OPTIONS
  450.      Control the format of the output produced by `disp' and Octave's
  451.      normal echoing mechanism.  Valid options are listed in the
  452.      following table.
  453.     `short'
  454.           This is the default format.  Octave will try to print numbers
  455.           with at least 5 significant figures within a field that is a
  456.           maximum of 10 characters wide.
  457.           If Octave is unable to format a matrix so that columns line
  458.           up on the decimal point and all the numbers fit within the
  459.           maximum field width, it switches to an `e' format.
  460.     `long'
  461.           Octave will try to print numbers with at least 15 significant
  462.           figures within a field that is a maximum of 24 characters
  463.           wide.
  464.           As will the `short' format, Octave will switch to an `e'
  465.           format if it is unable to format a matrix so that columns
  466.           line up on the decimal point and all the numbers fit within
  467.           the maximum field width.
  468.     `long e'
  469.     `short e'
  470.           The same as `format long' or `format short' but always display
  471.           output with an `e' format.  For example, with the `short e'
  472.           format, pi is displayed as `3.14e+00'.
  473.     `long E'
  474.     `short E'
  475.           The same as `format long e' or `format short e' but always
  476.           display output with an uppercase `E' format.  For example,
  477.           with the `long E' format, pi is displayed as
  478.           `3.14159265358979E+00'.
  479.     `free'
  480.     `none'
  481.           Print output in free format, without trying to line up
  482.           columns of matrices on the decimal point.  This also causes
  483.           complex numbers to be formatted like this `(0.604194,
  484.           0.607088)' instead of like this `0.60419 + 0.60709i'.
  485.     `bank'
  486.           Print in a fixed format with two places to the right of the
  487.           decimal point.
  488.     `+'
  489.           Print a `+' symbol for nonzero matrix elements and a space
  490.           for zero matrix elements.  This format can be very useful for
  491.           examining the structure of a large matrix.
  492.     `hex'
  493.           Print the hexadecimal representation numbers as they are
  494.           stored in memory.  For example, on a workstation which stores
  495.           8 byte real values in IEEE format with the least significant
  496.           byte first, the value of `pi' when printed in `hex' format is
  497.           `400921fb54442d18'.  This format only works for numeric
  498.           values.
  499.     `bit'
  500.           Print the bit representation of numbers as stored in memory.
  501.           For example, the value of `pi' is
  502.                01000000000010010010000111111011
  503.                01010100010001000010110100011000
  504.           (shown here in two 32 bit sections for typesetting purposes)
  505.           when printed in bit format on a workstation which stores 8
  506.           byte real values in IEEE format with the least significant
  507.           byte first.  This format only works for numeric types.
  508.  - Built-in Variable: print_answer_id_name
  509.      If the value of `print_answer_id_name' is nonzero, variable names
  510.      are printed along with the result.  Otherwise, only the result
  511.      values are printed.  The default value is 1.
  512. File: octave,  Node: Terminal Input,  Next: Simple File I/O,  Prev: Terminal Output,  Up: Basic Input and Output
  513. Terminal Input
  514. --------------
  515.    Octave has three functions that make it easy to prompt users for
  516. input.  The `input' and `menu' functions are normally used for managing
  517. an interactive dialog with a user, and the `keyboard' function is
  518. normally used for doing simple debugging.
  519.  - Built-in Function:  input (PROMPT)
  520.  - Built-in Function:  input (PROMPT, "s")
  521.      Print a prompt and wait for user input.  For example,
  522.           input ("Pick a number, any number! ")
  523.      prints the prompt
  524.           Pick a number, any number!
  525.      and waits for the user to enter a value.  The string entered by
  526.      the user is evaluated as an expression, so it may be a literal
  527.      constant, a variable name, or any other valid expression.
  528.      Currently, `input' only returns one value, regardless of the number
  529.      of values produced by the evaluation of the expression.
  530.      If you are only interested in getting a literal string value, you
  531.      can call `input' with the character string `"s"' as the second
  532.      argument.  This tells Octave to return the string entered by the
  533.      user directly, without evaluating it first.
  534.      Because there may be output waiting to be displayed by the pager,
  535.      it is a good idea to always call `fflush (stdout)' before calling
  536.      `input'.  This will ensure that all pending output is written to
  537.      the screen before your prompt.  *Note Input and Output::.
  538.  - Function File:  menu (TITLE, OPT1, ...)
  539.      Print a title string followed by a series of options.  Each option
  540.      will be printed along with a number.  The return value is the
  541.      number of the option selected by the user.  This function is
  542.      useful for interactive programs.  There is no limit to the number
  543.      of options that may be passed in, but it may be confusing to
  544.      present more than will fit easily on one screen.
  545.  - Built-in Function:  keyboard (PROMPT)
  546.      This function is normally used for simple debugging.  When the
  547.      `keyboard' function is executed, Octave prints a prompt and waits
  548.      for user input.  The input strings are then evaluated and the
  549.      results are printed.  This makes it possible to examine the values
  550.      of variables within a function, and to assign new values to
  551.      variables.  No value is returned from the `keyboard' function, and
  552.      it continues to prompt for input until the user types `quit', or
  553.      `exit'.
  554.      If `keyboard' is invoked without any arguments, a default prompt of
  555.      `debug> ' is used.
  556.    For both `input' and `keyboard', the normal command line history and
  557. editing functions are available at the prompt.
  558.    Octave also has a function that makes it possible to get a single
  559. character from the keyboard without requiring the user to type a
  560. carriage return.
  561.  - Built-in Function:  kbhit ()
  562.      Read a single keystroke from the keyboard.  For example,
  563.           x = kbhit ();
  564.      will set X to the next character typed at the keyboard as soon as
  565.      it is typed.
  566. File: octave,  Node: Simple File I/O,  Prev: Terminal Input,  Up: Basic Input and Output
  567. Simple File I/O
  568. ---------------
  569.    The `save' and `load' commands allow data to be written to and read
  570. from disk files in various formats.  The default format of files
  571. written by the `save' command can be controlled using the built-in
  572. variables `default_save_format' and `save_precision'.
  573.    Note that Octave can not yet save or load structure variables or any
  574. user-defined types.
  575.  - Command: save OPTIONS FILE V1 V2 ...
  576.      Save the named variables V1, V2, ... in the file FILE.  The
  577.      special filename `-' can be used to write the output to your
  578.      terminal.  If no variable names are listed, Octave saves all the
  579.      variables in the current scope.  Valid options for the `save'
  580.      command are listed in the following table.  Options that modify
  581.      the output format override the format specified by the built-in
  582.      variable `default_save_format'.
  583.     `-ascii'
  584.           Save the data in Octave's text data format.
  585.     `-binary'
  586.           Save the data in Octave's binary data format.
  587.     `-float-binary'
  588.           Save the data in Octave's binary data format but only using
  589.           single precision.  You should use this format only if you
  590.           know that all the values to be saved can be represented in
  591.           single precision.
  592.     `-mat-binary'
  593.           Save the data in MATLAB's binary data format.
  594.     `-save-builtins'
  595.           Force Octave to save the values of built-in variables too.
  596.           By default, Octave does not save built-in variables.
  597.      The list of variables to save may include wildcard patterns
  598.      containing the following special characters:
  599.     `?'
  600.           Match any single character.
  601.     `*'
  602.           Match zero or more characters.
  603.     `[ LIST ]'
  604.           Match the list of characters specified by LIST.  If the first
  605.           character is `!' or `^', match all characters except those
  606.           specified by LIST.  For example, the pattern `[a-zA-Z]' will
  607.           match all lower and upper case alphabetic characters.
  608.      Except when using the MATLAB binary data file format, saving global
  609.      variables also saves the global status of the variable, so that if
  610.      it is restored at a later time using `load', it will be restored
  611.      as a global variable.
  612.      The command
  613.           save -binary data a b*
  614.      saves the variable `a' and all variables beginning with `b' to the
  615.      file `data' in Octave's binary format.
  616.    There are two variables that modify the behavior of `save'.
  617.  - Built-in Variable: default_save_format
  618.      This variable specifies the default format for the `save' command.
  619.      It should have one of the following values: `"ascii"', `"binary"',
  620.      `float-binary', or `"mat-binary"'.  The initial default save
  621.      format is Octave's text format.
  622.  - Built-in Variable: save_precision
  623.      This variable specifies the number of digits to keep when saving
  624.      data in text format.  The default value is 17.
  625.  - Command: load OPTIONS FILE V1 V2 ...
  626.      Load the named variables from the file FILE.  As with `save', you
  627.      may specify a list of variables and `load' will only extract those
  628.      variables with names that match.  For example, to restore the
  629.      variables saved in the file `data', use the command
  630.           load data
  631.      Octave will refuse to overwrite existing variables unless you use
  632.      the option `-force'.
  633.      If a variable that is not marked as global is loaded from a file
  634.      when a global symbol with the same name already exists, it is
  635.      loaded in the global symbol table.  Also, if a variable is marked
  636.      as global in a file and a local symbol exists, the local symbol is
  637.      moved to the global symbol table and given the value from the
  638.      file.  Since it seems that both of these cases are likely to be
  639.      the result of some sort of error, they will generate warnings.
  640.      The `load' command can read data stored in Octave's text and
  641.      binary formats, and MATLAB's binary format.  It will automatically
  642.      detect the type of file and do conversion from different floating
  643.      point formats (currently only IEEE big and little endian, though
  644.      other formats may added in the future).
  645.      Valid options for `load' are listed in the following table.
  646.     `-force'
  647.           Force variables currently in memory to be overwritten by
  648.           variables with the same name found in the file.
  649.     `-ascii'
  650.           Force Octave to assume the file is in Octave's text format.
  651.     `-binary'
  652.           Force Octave to assume the file is in Octave's binary format.
  653.     `-mat-binary'
  654.           Force Octave to assume the file is in MATLAB's binary format.
  655. File: octave,  Node: C-Style I/O Functions,  Prev: Basic Input and Output,  Up: Input and Output
  656. C-Style I/O Functions
  657. =====================
  658.    Octave's C-style input and output functions provide most of the
  659. functionality of the C programming language's standard I/O library.  The
  660. argument lists for some of the input functions are slightly different,
  661. however, because Octave has no way of passing arguments by reference.
  662.    In the following, FILE refers to a file name and `fid' refers to an
  663. integer file number, as returned by `fopen'.
  664.    There are three files that are always available.  Although these
  665. files can be accessed using their corresponding numeric file ids, you
  666. should always use the symbolic names given in the table below, since it
  667. will make your programs easier to understand.
  668.  - Built-in Variable: stdin
  669.      The standard input stream (file id 0).  When Octave is used
  670.      interactively, this is filtered through the command line editing
  671.      functions.
  672.  - Built-in Variable: stdout
  673.      The standard output stream (file id 1).  Data written to the
  674.      standard output is normally filtered through the pager.
  675.  - Built-in Variable: stderr
  676.      The standard error stream (file id 2).  Even if paging is turned
  677.      on, the standard error is not sent to the pager.  It is useful for
  678.      error messages and prompts.
  679. * Menu:
  680. * Opening and Closing Files::
  681. * Simple Output::
  682. * Line-Oriented Input::
  683. * Formatted Output::
  684. * Output Conversion for Matrices::
  685. * Output Conversion Syntax::
  686. * Table of Output Conversions::
  687. * Integer Conversions::
  688. * Floating-Point Conversions::  Other Output Conversions::
  689. * Other Output Conversions::
  690. * Formatted Input::
  691. * Input Conversion Syntax::
  692. * Table of Input Conversions::
  693. * Numeric Input Conversions::
  694. * String Input Conversions::
  695. * Binary I/O::
  696. * Temporary Files::
  697. * EOF and Errors::
  698. * File Positioning::
  699. File: octave,  Node: Opening and Closing Files,  Next: Simple Output,  Prev: C-Style I/O Functions,  Up: C-Style I/O Functions
  700. Opening and Closing Files
  701. -------------------------
  702.  - Built-in Function: [FID, MSG] = fopen (NAME, MODE, ARCH)
  703.  - Built-in Function: FID_LIST = fopen ("all")
  704.  - Built-in Function: FILE = fopen (FID)
  705.      The first form of the `fopen' function opens the named file with
  706.      the specified mode (read-write, read-only, etc.) and architecture
  707.      interpretation (IEEE big endian, IEEE little endian, etc.), and
  708.      returns an integer value that may be used to refer to the file
  709.      later.  If an error occurs, FID is set to -1 and MSG contains the
  710.      corresponding system error message.  The MODE is a one or two
  711.      character string that specifies whether the file is to be opened
  712.      for reading, writing, or both.
  713.      The second form of the `fopen' function returns a vector of file
  714.      ids corresponding to all the currently open files, excluding the
  715.      `stdin', `stdout', and `stderr' streams.
  716.      The third form of the `fopen' function returns the name of a
  717.      currently open file given its file id.
  718.      For example,
  719.           myfile = fopen ("splat.dat", "r", "ieee-le");
  720.      opens the file `splat.dat' for reading.  If necessary, binary
  721.      numeric values will be read assuming they are stored in IEEE
  722.      format with the least significant bit first, and then converted to
  723.      the native representation.
  724.      Opening a file that is already open simply opens it again and
  725.      returns a separate file id.  It is not an error to open a file
  726.      several times, though writing to the same file through several
  727.      different file ids may produce unexpected results.
  728.      The possible values `mode' may have are
  729.     `r'
  730.           Open a file for reading.
  731.     `w'
  732.           Open a file for writing.  The previous contents are discared.
  733.     `a'
  734.           Open or create a file for writing at the end of the file.
  735.     `r+'
  736.           Open an existing file for reading and writing.
  737.     `w+'
  738.           Open a file for reading or writing.  The previous contents are
  739.           discarded.
  740.     `a+'
  741.           Open or create a file for reading or writing at the end of the
  742.           file.
  743.      The parameter ARCH is a string specifying the default data format
  744.      for the file.  Valid values for ARCH are:
  745.           `native' The format of the current machine (this is the
  746.           default).
  747.           `ieee-le' IEEE big endian format.
  748.           `ieee-be' IEEE little endian format.
  749.           `vaxd' VAX D floating format.
  750.           `vaxg' VAX G floating format.
  751.           `cray' Cray floating format.
  752.      however, conversions are currently only supported for `native'
  753.      `ieee-be', and `ieee-le' formats.
  754.  - Built-in Function:  fclose (FID)
  755.      Closes the specified file.  If an error is encountered while
  756.      trying to close the file, an error message is printed and `fclose'
  757.      returns 0.  Otherwise, it returns 1.
  758. File: octave,  Node: Simple Output,  Next: Line-Oriented Input,  Prev: Opening and Closing Files,  Up: C-Style I/O Functions
  759. Simple Output
  760. -------------
  761.  - Built-in Function:  fputs (FID, STRING)
  762.      Write a string to a file with no formatting.
  763.  - Built-in Function:  puts (STRING)
  764.      Write a string to the standard output with no formatting.
  765. File: octave,  Node: Line-Oriented Input,  Next: Formatted Output,  Prev: Simple Output,  Up: C-Style I/O Functions
  766. Line-Oriented Input
  767. -------------------
  768.  - Built-in Function:  fgetl (FID, LEN)
  769.      Read characters from a file, stopping at the first newline
  770.      character that is encountered or after LEN characters have been
  771.      read, and returning the characters as a string.  The newline is
  772.      not included in the returned value.
  773.      If LEN is omitted, `fgetl' reads until the next newline character.
  774.      If there are no more characters to read, `fgetl' returns -1.
  775.  - Built-in Function:  fgets (FID, LEN)
  776.      Read characters from a file, stopping at the first newline
  777.      character that is encountered or after LEN characters have been
  778.      read, and returning the characters as a string.  The newline is
  779.      included in the returned value.
  780.      If LEN is omitted, `fgets' reads until the next newline character.
  781.      If there are no more characters to read, `fgets' returns -1.
  782. File: octave,  Node: Formatted Output,  Next: Output Conversion for Matrices,  Prev: Line-Oriented Input,  Up: C-Style I/O Functions
  783. Formatted Output
  784. ----------------
  785.    This section describes how to call `printf' and related functions.
  786.    The following functions are available for formatted output.  They are
  787. modelled after the C language functions of the same name, but they
  788. interpret the format template differently in order to improve the
  789. performance of printing vector and matrix values.
  790.  - Function File:  printf (TEMPLATE, ...)
  791.      The `printf' function prints the optional arguments under the
  792.      control of the template string TEMPLATE to the stream `stdout'.
  793.  - Built-in Function:  fprintf (FID, TEMPLATE, ...)
  794.      This function is just like `printf', except that the output is
  795.      written to the stream FID instead of `stdout'.
  796.  - Built-in Function:  sprintf (TEMPLATE, ...)
  797.      This is like `printf', except that the output is returned as a
  798.      string.  Unlike the C library function, which requires you to
  799.      provide a suitably sized string as an argument, Octave's `sprintf'
  800.      function returns the string, automatically sized to hold all of
  801.      the items converted.
  802.    The `printf' function can be used to print any number of arguments.
  803. The template string argument you supply in a call provides information
  804. not only about the number of additional arguments, but also about their
  805. types and what style should be used for printing them.
  806.    Ordinary characters in the template string are simply written to the
  807. output stream as-is, while "conversion specifications" introduced by a
  808. `%' character in the template cause subsequent arguments to be
  809. formatted and written to the output stream.  For example,
  810.      pct = 37;
  811.      filename = "foo.txt";
  812.      printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
  813.              filename, pct);
  814. produces output like
  815.      Processing of `foo.txt' is 37% finished.
  816.      Please be patient.
  817.    This example shows the use of the `%d' conversion to specify that a
  818. scalar argument should be printed in decimal notation, the `%s'
  819. conversion to specify printing of a string argument, and the `%%'
  820. conversion to print a literal `%' character.
  821.    There are also conversions for printing an integer argument as an
  822. unsigned value in octal, decimal, or hexadecimal radix (`%o', `%u', or
  823. `%x', respectively); or as a character value (`%c').
  824.    Floating-point numbers can be printed in normal, fixed-point notation
  825. using the `%f' conversion or in exponential notation using the `%e'
  826. conversion.  The `%g' conversion uses either `%e' or `%f' format,
  827. depending on what is more appropriate for the magnitude of the
  828. particular number.
  829.    You can control formatting more precisely by writing "modifiers"
  830. between the `%' and the character that indicates which conversion to
  831. apply.  These slightly alter the ordinary behavior of the conversion.
  832. For example, most conversion specifications permit you to specify a
  833. minimum field width and a flag indicating whether you want the result
  834. left- or right-justified within the field.
  835.    The specific flags and modifiers that are permitted and their
  836. interpretation vary depending on the particular conversion.  They're all
  837. described in more detail in the following sections.
  838. File: octave,  Node: Output Conversion for Matrices,  Next: Output Conversion Syntax,  Prev: Formatted Output,  Up: C-Style I/O Functions
  839. Output Conversion for Matrices
  840. ------------------------------
  841.    When given a matrix value, Octave's formatted output functions cycle
  842. through the format template until all the values in the matrix have been
  843. printed.  For example,
  844.      printf ("%4.2f %10.2e %8.4g\n", hilb (3));
  845.      
  846.           -| 1.00   5.00e-01   0.3333
  847.           -| 0.50   3.33e-01     0.25
  848.           -| 0.33   2.50e-01      0.2
  849.    If more than one value is to be printed in a single call, the output
  850. functions do not return to the beginning of the format template when
  851. moving on from one value to the next.  This can lead to confusing output
  852. if the number of elements in the matrices are not exact multiples of the
  853. number of conversions in the format template.  For example,
  854.      printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]);
  855.      
  856.           -| 1.00   2.00e+00        3
  857.           -| 4.00
  858.    If this is not what you want, use a series of calls instead of just
  859. File: octave,  Node: Output Conversion Syntax,  Next: Table of Output Conversions,  Prev: Output Conversion for Matrices,  Up: C-Style I/O Functions
  860. Output Conversion Syntax
  861. ------------------------
  862.    This section provides details about the precise syntax of conversion
  863. specifications that can appear in a `printf' template string.
  864.    Characters in the template string that are not part of a conversion
  865. specification are printed as-is to the output stream.
  866.    The conversion specifications in a `printf' template string have the
  867. general form:
  868.      % FLAGS WIDTH [ . PRECISION ] TYPE CONVERSION
  869.    For example, in the conversion specifier `%-10.8ld', the `-' is a
  870. flag, `10' specifies the field width, the precision is `8', the letter
  871. `l' is a type modifier, and `d' specifies the conversion style.  (This
  872. particular type specifier says to print a numeric argument in decimal
  873. notation, with a minimum of 8 digits left-justified in a field at least
  874. 10 characters wide.)
  875.    In more detail, output conversion specifications consist of an
  876. initial `%' character followed in sequence by:
  877.    * Zero or more "flag characters" that modify the normal behavior of
  878.      the conversion specification.
  879.    * An optional decimal integer specifying the "minimum field width".
  880.      If the normal conversion produces fewer characters than this, the
  881.      field is padded with spaces to the specified width.  This is a
  882.      *minimum* value; if the normal conversion produces more characters
  883.      than this, the field is *not* truncated.  Normally, the output is
  884.      right-justified within the field.
  885.      You can also specify a field width of `*'.  This means that the
  886.      next argument in the argument list (before the actual value to be
  887.      printed) is used as the field width.  The value is rounded to the
  888.      nearest integer.  If the value is negative, this means to set the
  889.      `-' flag (see below) and to use the absolute value as the field
  890.      width.
  891.    * An optional "precision" to specify the number of digits to be
  892.      written for the numeric conversions.  If the precision is
  893.      specified, it consists of a period (`.') followed optionally by a
  894.      decimal integer (which defaults to zero if omitted).
  895.      You can also specify a precision of `*'.  This means that the next
  896.      argument in the argument list (before the actual value to be
  897.      printed) is used as the precision.  The value must be an integer,
  898.      and is ignored if it is negative.
  899.    * An optional "type modifier character".  This character is ignored
  900.      by Octave's `printf' function, but is recognized to provide
  901.      compatibility with the C language `printf'.
  902.    * A character that specifies the conversion to be applied.
  903.    The exact options that are permitted and how they are interpreted
  904. vary between the different conversion specifiers.  See the descriptions
  905. of the individual conversions for information about the particular
  906. options that they use.
  907. File: octave,  Node: Table of Output Conversions,  Next: Integer Conversions,  Prev: Output Conversion Syntax,  Up: C-Style I/O Functions
  908. Table of Output Conversions
  909. ---------------------------
  910.    Here is a table summarizing what all the different conversions do:
  911. `%d', `%i'
  912.      Print an integer as a signed decimal number.  *Note Integer
  913.      Conversions::, for details.  `%d' and `%i' are synonymous for
  914.      output, but are different when used with `scanf' for input (*note
  915.      Table of Input Conversions::.).
  916.      Print an integer as an unsigned octal number.  *Note Integer
  917.      Conversions::, for details.
  918.      Print an integer as an unsigned decimal number.  *Note Integer
  919.      Conversions::, for details.
  920. `%x', `%X'
  921.      Print an integer as an unsigned hexadecimal number.  `%x' uses
  922.      lower-case letters and `%X' uses upper-case.  *Note Integer
  923.      Conversions::, for details.
  924.      Print a floating-point number in normal (fixed-point) notation.
  925.      *Note Floating-Point Conversions::, for details.
  926. `%e', `%E'
  927.      Print a floating-point number in exponential notation.  `%e' uses
  928.      lower-case letters and `%E' uses upper-case.  *Note Floating-Point
  929.      Conversions::, for details.
  930. `%g', `%G'
  931.      Print a floating-point number in either normal (fixed-point) or
  932.      exponential notation, whichever is more appropriate for its
  933.      magnitude.  `%g' uses lower-case letters and `%G' uses upper-case.
  934.      *Note Floating-Point Conversions::, for details.
  935.      Print a single character.  *Note Other Output Conversions::.
  936.      Print a string.  *Note Other Output Conversions::.
  937.      Print a literal `%' character.  *Note Other Output Conversions::.
  938.    If the syntax of a conversion specification is invalid, unpredictable
  939. things will happen, so don't do this.  If there aren't enough function
  940. arguments provided to supply values for all the conversion
  941. specifications in the template string, or if the arguments are not of
  942. the correct types, the results are unpredictable.  If you supply more
  943. arguments than conversion specifications, the extra argument values are
  944. simply ignored; this is sometimes useful.
  945.